<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
	response.setHeader("Pragma", "No-cache");
	response.setHeader("Cache-Control", "no-cache");
	response.setDateHeader("Expires", 0);
%>
<!-- 版本3.0  -->
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>上传文件</title>
<jsp:include page="../common/top.jsp"></jsp:include>
</head>
<body class="page-content ">
	<div class="page-content-area" data-ajax-content="true">
		<div class="page-header">
			<h1>上传文件</h1>
		</div>
		<div class="row">
			<div class="col-xs-12">
				<form id="_editForm" class="form-horizontal" role="form"
					validate="true" action="${path }/admin/sysFile/uploadFile"
					method="post" enctype="multipart/form-data">
					<div class="form-group">
						<!-- <label class="col-sm-3 control-label no-padding-right">上传文件</label>
							<div class="col-sm-9">
								<button class="btn btn-xs btn-warning" id="_downloadButton">
									<i class="ace-icon fa fa-cloud-download bigger-110"></i>
									<span class="bigger-110 no-text-shadow">下载模板</span>
								</button>
							</div> -->
					</div>
					<div>

						<input type="hidden" name="id" value="${bean.id }"/> 
						
					<div class="form-group">
						<label class="col-sm-3 control-label no-padding-right">文件类型</label>
						<div class="col-sm-9">
							<div class="clearfix help-validate">
								<input type="text" name="type" value="${bean.type}"
									placeholder="文件类型" class="col-xs-10 col-sm-5">
								<li style="color: red;">必填</li>
							</div>
						</div>
					</div>
						
					<div class="form-group">
						<label class="col-sm-3 control-label no-padding-right">文件作者</label>
						<div class="col-sm-9">
							<div class="clearfix help-validate">
								<input type="text" name="author" value="${bean.author}"
									placeholder="作者" class="col-xs-10 col-sm-5">
								<li style="color: red;">必填</li>
							</div>
						</div>
					</div>
					
					</div>

					<div class="form-group">
						<label class="col-sm-3 control-label no-padding-right"></label>
						<div class="col-sm-9">
							<div class="clearfix help-validate">
								<input type="file" name="file"  id="file" required="true"  >
							</div>
						</div>
					</div>
					
					<%-- <div class="form-group" id="videoHidden"
					<c:if test="${bean.fileUrl }">style="display:block;"</c:if>>
						<label class="col-sm-3 control-label no-padding-right">文件</label>
						<div class="row">
							<div class="col-sm-3">
								<div class="clearfix help-validate">
									<input type="file" id="file" name="file"
										onchange="fileChange(this);" placeholder="文件"> <input
										type="hidden" id="up-file-hide" name="fileUrl"
										value="${bean.fileUrl }">
								</div>
							</div>
							<div class="col-sm-3">
								<button class="btn btn-xs" id="submit_upload">上传</button>
								<span style="color: red">必填</span>
							</div>
						</div> --%>
					
					<div class="clearfix form-actions">
						<div class="col-md-offset-3 col-md-9">
							<button id="_submit" class="btn btn-info" type="button"
								data-loading-text="正在上传..." permission="/question/save">
								<i class="ace-icon fa fa-check bigger-110"></i> 上传
							</button>
						</div>
					</div>
				</form>
			</div>
		</div>
	</div>
	<jsp:include page="../common/bottom.jsp"></jsp:include>
	<script src="${path}/script/jquery.form.js"></script>
	<script type="text/javascript">
		jQuery(function($) {
			//提交表单
			$("#_editForm").ajaxForm({
				dataType : 'json',
				success : function(d) {
					$("#_submit").button('reset');
					if (d.status == "0000") {
						window.location.href = "${path }/admin/sysFile";
					} else {
						$.gritter.add({
							text : d.message
						});
					}
				}
			});

			$("#_submit").click(function() {
				if ($('#_editForm').validate()) {
					var file = $('input[name="file"]').val();
					/* if (file == '') {
						alert('请选择上传的文件');
						return;
					} */
					var btn = $(this);
					btn.button('loading');
					$("#_editForm").submit();
				}
			});
		});
	</script>
	
	<script type="text/javascript">
	//上传
		$("#submit_upload").click(function(e) {
			e.preventDefault()
			var formData = new FormData($("#_editForm")[0]);
			console.log(formData)
			$.ajax({
				url : '${path}/common/upload/springUpload',
				type : 'POST',
				data : formData,
				async : false,
				cache : false,
				contentType : false,
				processData : false,
				success : function(result) {
					$('#up-file-hide').val(result.data.filePath);
					$('#fileSize').val(result.data.fileSize)
					$('#fileName').val(result.data.fileName1)
					$("#submit_upload").attr('disabled', true)
				}
			});
		});

		function fileChange(target) {
			//检测上传文件的类型
			var imgName = document.all.file.value;
			var ext, idx;
			if (imgName == '') {
				$('#submit_upload').attr('disabled', true)
				$.gritter.add("请选择需要上传的文件!");
				return;
			} else {
				idx = imgName.lastIndexOf(".");
				if (idx != -1) {
					ext = imgName.substr(idx + 1).toUpperCase();
					ext = ext.toLowerCase();
					if (ext != 'zip' && ext != 'doc' && ext != 'docx'
							&& ext != 'xls' && ext != 'txt' && ext != 'ppt') {
						$('#submit_upload').attr('disabled', true)
						$.gritter.add("只能上传.zip .doc  .docx  .xls .txt .ppt类型的文件!");
						return;
					}
				} else {
					$('#submit_upload').attr('disabled', true)
					alert("只能上传.zip   .doc  .docx  .xls .txt .ppt类型的文件!");
					return;
				}
			}

			//检测上传文件的大小
			var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
			var fileSize = 0;
			if (isIE && !target.files) {
				var filePath = target.value;
				var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
				var file = fileSystem.GetFile(filePath);
				fileSize = file.Size;
			} else {
				fileSize = target.files[0].size;
			}

			var size = fileSize / 1024 * 1024;

			if (size > (1024 * 1024 * 10)) {
				$('#submit_upload').attr('disabled', true)
				$.gritter.add("文件大小不能超过10MB");
			} else {
				$('#submit_upload').attr('disabled', false)
			}
		}
	</script>
</body>
</html>

